package cn.cvs.service.sysUser;

import cn.cvs.mapper.sysUser.SysUserMapper;
import cn.cvs.pojo.SysRole;
import cn.cvs.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

/**
 * 用户模块业务层实现
 */
@Transactional
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public SysUser selectByAccount(String account) {
        return sysUserMapper.selectByAccount(account);
    }

    @Override
    public SysUser login(String account, String password) {
        return sysUserMapper.login(account,password);
    }

    @Override
    public List<SysUser> getList(SysUser sysUser) {
        try {
            return sysUserMapper.selectUserList(sysUser);
        }catch (RuntimeException e) {
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public boolean add(SysUser sysUser) {
        boolean result = false;
        try {
            if(sysUserMapper.add(sysUser) == 1){
                result = true;
                //测试事务回滚时，打开注释
                //throw new RuntimeException();
            }
        }catch (RuntimeException e) {
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    @Override
    public boolean delete(Integer id) {
        boolean result = false;
        try {
            if(sysUserMapper.delete(id) == 1){
                result = true;
                //测试事务回滚时，打开注释
                //throw new RuntimeException();
            }
        }catch (RuntimeException e) {
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    @Override
    public int updatePwd(SysUser user) {
        return sysUserMapper.updatePwd(user);
    }

    @Override
    public boolean update(SysUser user) {
        boolean result = false;
        try {
            if(sysUserMapper.update(user) == 1){
                result = true;
                //测试事务回滚时，打开注释
                //throw new RuntimeException();
            }
        }catch (RuntimeException e) {
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    @Override
    public List<SysUser> selectList(String realName, Integer roleId, Integer pageBegin, Integer pageSize) {
        return sysUserMapper.selectList(realName, roleId, pageBegin, pageSize);
    }

    @Override
    public SysUser getUserById(Integer id) {
        return sysUserMapper.getUserById(id);
    }

    @Override
    public int getUserCount(String queryRealName, Integer queryRoleId) {
        return sysUserMapper.getUserCount(queryRealName, queryRoleId);
    }

    @Override
    public List<SysUser> getUserList(String queryRealName, Integer queryRoleId, Integer pageIndex, int pageSize) {
        pageIndex = (pageIndex - 1) * pageSize;
        return sysUserMapper.getUserList(queryRealName, queryRoleId, pageIndex, pageSize);
    }


    public SysUserMapper getSysUserMapper() {
        return sysUserMapper;
    }

    public void setSysUserMapper(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }
}
